local assigned = redis.call("hget", "weightAssignMap", KEYS[1])
if assigned == false then
    local exist = redis.call("exists", "weightList")
    if exist == 0 then
        return 0
    end
    local rand = tonumber(KEYS[2])
    local table = redis.call("lrange", "weightList", 0, -1)
    local i = 1
    while (rand >= tonumber(table[i]))
    do
        rand = rand - tonumber(table[i])
        i = i + 1
    end
    redis.call("hset", "weightAssignMap", KEYS[1], i)
    return i - 1
else
    return tonumber(assigned)
end
